// import com.alibaba.druid.DbType;
// import com.alibaba.druid.sql.SQLUtils;
// import com.alibaba.druid.sql.ast.SQLExpr_ptr;
// import com.alibaba.druid.sql.ast.SQLName_ptr;
// import com.alibaba.druid.sql.ast.SQLObject;
// import com.alibaba.druid.sql.ast.SQLStatementImpl;
// import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
// import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
// import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "SQLCreateTableGroupStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/instanceof.h"
#include "../../SQLUtils.h"
#include "../expr/SQLCharExpr.h"

SQLCreateTableGroupStatement::SQLCreateTableGroupStatement()
{
}

SQLCreateTableGroupStatement::SQLCreateTableGroupStatement(DbType_ptr dbType)
    : SQLStatementImpl(dbType)
{
}

string_ptr SQLCreateTableGroupStatement::getSchemaName()
{
  if (instanceof <SQLName, SQLPropertyExpr>(name))
  {
    return SQLUtils::toMySqlString((std::dynamic_pointer_cast<SQLPropertyExpr>(name))->getOwner());
  }
  return nullptr;
}

void SQLCreateTableGroupStatement::setSchemaName(string_ptr name)
{
  if (name != nullptr)
  {
    this->name = SQLPropertyExpr_ptr(new SQLPropertyExpr(name, getTableGroupName()));
  }
}

string_ptr SQLCreateTableGroupStatement::getTableGroupName()
{
  if (instanceof <SQLName, SQLPropertyExpr>(name))
  {
    return (std::dynamic_pointer_cast<SQLPropertyExpr>(name))->getName();
  }
  else if (instanceof <SQLName, SQLIdentifierExpr>(name))
  {
    return (std::dynamic_pointer_cast<SQLIdentifierExpr>(name))->getName();
  }
  else if (instanceof <SQLName, SQLCharExpr>(name))
  {
    return (std::dynamic_pointer_cast<SQLCharExpr>(name))->getText();
  }
  return nullptr;
}

SQLExpr_ptr SQLCreateTableGroupStatement::getPartitionNum()
{
  return partitionNum;
}

void SQLCreateTableGroupStatement::setPartitionNum(SQLExpr_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLCreateTableGroupStatement));
  }
  this->partitionNum = x;
}

// @Override
void SQLCreateTableGroupStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLCreateTableGroupStatement)))
  {
    acceptChild(visitor, name);
  }
  visitor->endVisit(SharedObject(SQLCreateTableGroupStatement));
}

// @Override
SQLObject_list_ptr SQLCreateTableGroupStatement::getChildren()
{
  SQLObject_list_ptr children = std::make_shared<std::list<SQLObject_ptr>>();
  if (name != nullptr)
  {
    children->push_back(name);
  }
  return children;
}

SQLName_ptr SQLCreateTableGroupStatement::getName()
{
  return name;
}

void SQLCreateTableGroupStatement::setName(SQLName_ptr name)
{
  this->name = name;
}

BOOL_ptr SQLCreateTableGroupStatement::isIfNotExists()
{
  return ifNotExists;
}

void SQLCreateTableGroupStatement::setIfNotExists(BOOL_ptr ifNotExists)
{
  this->ifNotExists = ifNotExists;
}
